package com.me.common.util.security;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

/**
 * BASE64 编码、解码：拼接了自定义字符！！！
 */
public class Base64Utils {

    /** BASE64 固定拼接的前缀 */
    private static final String PREFIX_STR = "NjJGJXU4OTI5JXVGRjAx";
    /** BASE64 固定前缀的长度 */
    private static final int LENGTH = PREFIX_STR.length();

    /** 编码 */
    private static final Base64.Encoder ENCODER64 = Base64.getEncoder();
    /** 解码 */
    private static final Base64.Decoder DECODER64 = Base64.getDecoder();

    public static void main(String[] args) {
        String str = "2022-08-04";
        System.out.println("编码：");
        String encodeStr = encode(str);
        System.out.println(encodeStr);
        System.out.println("解码：");
        System.out.println(decode(encodeStr));
    }

    public static String encode(String str) {
        return PREFIX_STR + new String(ENCODER64.encode(str.getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8);
    }

    public static String decode(String str) {
        return new String(DECODER64.decode(str.substring(20).getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8);
    }

}
